<?php

namespace App\Models\Factory;

use App\Models\AbsModelFactory;
use App\Models\Orm\Identity;
use App\Models\Orm\Occupation;
use App\Models\Orm\UserStatus;
use App\Helpers\DateUtils;
use App\Helpers\Utils;
use App\Models\Factory\UserFactory;



/**
 * 身份工厂
 * Class SmsFactory
 * @package App\Models\Factory
 */
class IdentityFactory extends AbsModelFactory
{



    //根据电话获取信息
    public static function getdata($userphone)
    {
        $data = Identity::where('userphone',$userphone)->get()->toArray();
        return $data;
    }



    //获取用户身份填写是否完成
    public static  function getIdentity($userId)
    {

        $result = UserStatus::where('user_id',$userId)->first();
        if($result)
        {
           if($result->user_profile_status && $result->bank_status && $result->occupation_status)
           {
               return 1;
           }else{
               return 0;
           }
        }
        return 0;
    }


    public static  function getOccupation($userId)
    {
        $result = Occupation::where('uid',$userId)->first();
        if($result){
            return $result->status ? 1:0;

        }else{
            return 0;
        }
    }

    /**
     * 完善更新用户申请信息
     * @param  $Identitydata
     * @return $result
     */
    public static function createIdentity($Identitydata)
    {
        $identityObj = Identity::where('uid',$Identitydata['uid'])->first();
        if(empty($identityObj)){
            return false;
        }
        $identityObj ->is_credit    = intval($Identitydata['is_credit']);
        $identityObj ->has_house    = intval($Identitydata['has_house']);
        $identityObj ->has_car      = intval($Identitydata['has_car']);
        $identityObj ->professional = intval($Identitydata['professional']);        
        $result = $identityObj ->save();
        return $result ? : false;

    }

    /**
    * 更新用户身份证基本信息
    * @param  $Identitydata
    * @return $result
    */
    public static function setIdcard($Identitydata){
        // 声明用户基本身份信息
        $idcard    = $Identitydata['idcard'];
        $birthday  = Utils::getbirthdaybyidcard($idcard);
        $sex       = Utils::getsexbyidcard($idcard);
        $userphone = $Identitydata['userphone'];
        $user_id   = $Identitydata['user_id'];
        $name      = $Identitydata['name'];
        // 更新用户主表相关信息
        $userdata  = array('nickname' => $name, 'birthday' => strtotime($birthday),'sex' => $sex );
        $upres = UserFactory::setIdentity($user_id,$userdata);

        $identityObj = Identity::where('userphone',$userphone)->first();
        if(empty($identityObj)){
            $identityObj = new Identity();            
        }
        $identityObj ->uid       = intval($user_id);
        $identityObj ->userphone = $userphone;
        $identityObj ->name      = strval($name);
        $identityObj ->idcard    = $idcard;
        $identityObj ->age       = DateUtils::getAge(strtotime($birthday));
        $identityObj ->sex       = $sex;
        $identityObj ->birthday  = $birthday;
        $result = $identityObj ->save();
        return $result ? : false;
        
    }



    public static function addIdentity($userId,$data)
    {
        //查看数据库有没有这条数据,如果没有创建一条
        $identityObj = Identity::where('uid',$userId)->first();
        if(empty($identityObj))
        {
            $identityObj = new Identity();

        }
        $identityObj ->uid = $userId;
        $identityObj ->name = $data['identity']['name'];
        $identityObj ->userphone = $data['identity']['userphone'];
        $identityObj ->idcard  = $data['identity']['idcard'];
        $identityObj ->sex   = $data['identity']['sex'];
        $identityObj ->age   = $data['identity']['age'];
        $identityObj -> idaddress= $data['identity']['idaddress'];
        $identityObj -> marriage= $data['identity']['marriage'];
        $identityObj ->city    = $data['identity']['city'];
        $identityObj ->contact= json_encode($data['identity']['contact'],true);
        $identityObj ->istatus = $data['identity']['istatus'];
        $identityObj ->ilass_time = date("Y-m-d h:i:s",time());

        if($identityObj ->save())
        {
           return  true;
        }else{

            return false;
        }
    }


    public static  function  getIdent($userId,$user)
    {
        $identity = Identity::where('uid', $userId)->select('name','userphone','idcard','sex','age','idaddress','marriage','city','contact','ilass_time')->first();
        if ($identity) {
            $identity  = $identity ->toArray();
            $data1['isSuccess'] = "1";
            $data1['identity'] = $identity;
            $data1['identity']['userphone'] = $user -> userphone;
            $data1['identity']['contact'] = json_decode($identity['contact'], true) ?: [0 => ['contact' => '', 'relation' => '', 'userphone' => ''], 1 => ['contact' => '', 'relation' => '', 'userphone' => '']];
            $data1['status'] = "1";
            return $data1;
        } else {
            $data2['identity']['userphone'] = $user ->userphone;
            $data2['isSuccess'] = "1";
            $data2['identity']['name'] = "";
            $data2['identity']['idcard'] = "";
            $data2['identity']['sex'] = "";
            $data2['identity']['age'] = "";
            $data2['identity']['idaddress'] = "";
            $data2['identity']['city'] = "";
            $data2['identity']['marriage'] = "";
            $data2['identity']['contact'] = [0 => ["contact" => "", "relation" => "", "userphone" => ""], 1 => ["contact" => "", "relation" => "", "userphone" => ""]];
            $data2['identity']['ilass_time'] = "";
            $data2['status'] = "1";
            return $data2;
        }
    }

    //修改用户身份和昵称
    public static  function setIden($userId,$data)
    {
        $user = Identity::where('uid',$userId)->update($data);
        return $user ? true : false;
    }



}